<?php

require_once PATH_FFWD.'prototypes/module.base.php';

class BaseResourceModule extends BaseModule{
	public function checkAction($p){
		//begin check.php
		$fileArray = array();
		foreach ($_POST as $key => $value) {
			if ($key != 'folder') {
				if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_POST['folder'] . '/' . $value)) {
					$fileArray[$key] = $value;
				}
			}
		}
		echo json_encode($fileArray);
		//end check.php		
	}
	
	public function show(&$p){
		print_var($p,'show');
	}
	
	public function douploadAction($p){
		global $user,$app;
		obflush();
		$this->requireUser();
		
		$allowed_ext=Array( 'jpg','jpeg','flv','vmw','avi','mpeg' );
		
		file_put_contents('1.txt',var_export($p,1));
		
		if(isset($p['user_cookie']))
			$user->loginByCookie($p['user_cookie']);			
			
		file_put_contents('user.txt',(string)$user->name); 
		/*
		print_var($user,'user');
		print_var($p,'p');
		print_var($_FILES,'_FILES');/**/
		
		
		
		$this->requireUser();
			
		@$ext=file_get_extension($_POST['Filename']);
		$ext=strtolower($ext);
		if(!in_array($ext,$allowed_ext))die('invalid_extension: '.$ext);
		
		
		if($this->table){
			$e=$t_photo->getById('new');		
			$e->filename=$_POST['Filename'];
			
			$e->filesize=$_FILES['Filedata']['size'];
			$e->collection_id=$_GET['collection_id'];	
			$e->user_id=(string)$user->id;	
			$e->folder=$p['path'];
			$e->created_at='now()';
			$e->save();
		};
		
		$filename='upload/'.$p['path'].'/'.$_POST['Filename'];
		file_put_contents('filename',$filename);
		if(!is_dir(dirname($filename)))mkdir(dirname($filename),0666,1);
		
		move_uploaded_file($_FILES['Filedata']['tmp_name'],$filename);
				
		echo "OK";
		
		if(isset($_GET['__sender'])){
			$__sender=urldecode($_GET['__sender']);
			header('Location: '.$__sender,301);
			exit;
		}else{
			die(1);
		}
	}
	
	public function downloadAction(&$p){
		if(!@$p['id']){
			$this->lasterror='no_id_set';
			return 1;			
		};
		return $this->show($p);		
	}

	public function listAction(&$p){
		ob_end_clean();
		$this->requireUser();
		if(!isset($p['folder']))throw new Exception('Folder parameter must be defined');
		global $db,$controller;
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-cache");
		header("Pragma: no-cache");
		header("Content-type: text/html; charset=utf-8");
		echo '<link rel="stylesheet" type="text/css" href="/css/uploadify.css" >';
		$path='upload/'.$p['folder'];
		if(!is_dir($path))exit;
		
		$dir=opendir($path);
		$icons=glob(PATH_FFWD.'static/images/icons/*.png');foreach($icons as $ix=>$if)$icons[$ix]=fnwithoutext(basename($if));
		if(!isset($p['view']))$p['view']='icon';
		if($p['view']=='icon'){			
			while($f=readdir($dir)){
				if($f[0]=='.')continue;
				$fl=strtolower(fnext($f));
				if(!in_array($fl,$icons))$fl='unknown';
				echo '<a href="'.HREF_ROOT.'upload/'.$p['folder'].'/'.$f.'" target="_blank" class="resource_file ffwd_iconfile icon_'.$fl.'">';			
				echo $f;
				echo '</a>';
			}
		}else
		if($p['view']=='thumb'){			
			while($f=readdir($dir)){
				if($f[0]=='.')continue;
				echo '<a href="'.HREF_ROOT.'upload/'.$p['folder'].'/'.$f.'" target="_blank" class="resource_file ffwd_thumbfile">';
				$thumb_src='upload/'.$p['folder'].'/'.fnwithoutext($f).'_100x50.'.fnext($f);			
				echo '<img src="'.HREF_ROOT.$thumb_src.'" class="resource_img" /><br/>';
				echo $f;
				echo '</a>';
			}
		}
		closedir($dir);
		
		
		exit;	
		//$controller->shutdown();	
	}
	
	public function deleteAction(&$p){
		global $db;
		$this->requireUser();
		if(!isset($p['file']))throw new Exception('File parameter not defined');
		$fn=urldecode($p['file']);
		$fn=substr($fn,strlen(HREF_ROOT));
		$base=basename($fn);
		$folder=substr($fn,strlen('upload/'),-strlen($base)-1);		
		if($this->table){			
			$e=$this->table->getBy(Array('folder'=>$folder,'filename'=>$base));
			if($e)$e->delete();
		};
		$todel=Array($fn);$fnnoext=fnwithoutext($base);$fnext=fnext($fn);
		$pattern='upload/'.$folder.'/.'.$fnnoext.'_*.'.$fnext;$ff=glob($pattern);
		$rx='/upload\/'.str_replace('/','\\/',($folder)).'\/.'.$fnnoext.'_([0-9]*)x([0-9]*)\.'.$fnext.'/';
		foreach($ff as $f)if(preg_match($rx,$f))$todel[]=$f;
		foreach($todel as $f)unlink($f);		
		echo '0';		
		exit;				
	}
	
	public function postAction($p){
		?>
		<form method="post" action="upload.html" enctype="multipart/form-data" >
			<input type="text" name="folder" value="#content_1" />
			<input type="text" name="Filename" value="proba.txt" />
			<input type="file" name="Filedata" />
			<input type="submit" />
		</form> 
		<?php 
	}
}
